# 分析mqtt的日志中所有有坐标的，和无坐标的，限制时间
import json
from datetime import datetime
import math

# 加载 JSON 数据
with open(r"MqttCapacityTest\MQTTlog\391.json", "r") as file:
    data = json.load(file)

# 定义时间范围
start_time = datetime.strptime("2025-02-24 17:24:54.701069", "%Y-%m-%d %H:%M:%S.%f")
end_time = datetime.strptime("2025-02-24 17:31:28.707461", "%Y-%m-%d %H:%M:%S.%f")

# 初始化计数器
topic_count = 0
xyz_count = 0

# 遍历 messages
for connection in data:
    for message in connection.get("messages", []):
        # 解析 createAt 时间
        create_at = datetime.strptime(message["createAt"], "%Y-%m-%d %H:%M:%S:%f")
        
        # 检查 topic 和时间范围
        if message["topic"] == "dwm/node/50ad/uplink/location" and start_time <= create_at <= end_time:
            topic_count += 1
            
            # 解析 payload
            payload = json.loads(message["payload"])
            
            # 检查 payload 中是否有 position，并且 x, y, z 是浮点数且不是 NaN
            if "position" in payload:
                position = payload["position"]
                if all(key in position for key in ["x", "y", "z"]):
                    x = position["x"]
                    y = position["y"]
                    z = position["z"]
                    
                    # 检查 x, y, z 是否为浮点数且不是 NaN
                    if all(isinstance(coord, (int, float)) and not math.isnan(coord) for coord in [x, y, z]):
                        xyz_count += 1

# 输出结果
print(f"Topic 'dwm/node/50ad/uplink/location' 的数量: {topic_count}")
print(f"其中包含有效的 x, y, z 坐标（浮点数且不为 NaN）的数量: {xyz_count}")